﻿@inject IPopupService PopupService

@inject IHttpService _httpService
@inject NavigationManager NavigationManager
@inject IDataCacheService _dataCacheService

<MDCard Class="@($"{Class} pa-4")">
    <MCardText>
        <MTextField Label="附加贡献值" TValue="int" @bind-Value="Model.ContributionValue"></MTextField>
        <MTextarea Label="批注" Rows="3" @bind-Value="Model.Comments"/>
    </MCardText>
    <MCardActions>
        <CnGalWebSite.Components.Buttons.MasaButton Text="通过" IsAsync="true" OnClick="()=>OnExamine(true)" Icon="mdi-check" Rounded />
        <MSpacer />
        <CnGalWebSite.Components.Buttons.MasaButton Text="驳回" IsAsync="true" OnClick="()=>OnExamine(false)" Icon="mdi-close" TextStyle />
    </MCardActions>
</MDCard>


@code {
    [Parameter]
    public ExamineViewModel Model { get; set; }
    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }
    [Parameter]
    public string Class { get; set; }

    private async Task OnExamine(bool isPassed)
    {
        //上传
        try
        {
            if (Model.SensitiveWords != null && Model.SensitiveWords.Count != 0 && isPassed)
            {
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                foreach (var item in Model.SensitiveWords)
                {
                    if (Model.SensitiveWords.IndexOf(item) != Model.SensitiveWords.Count - 1)
                    {
                        sb.Append('，');
                    }

                    sb.Append(item);
                }

                //提醒敏感词
            }
            var obj = await _httpService.PostAsync<ExamineProcModel, Result>("api/examines/proc", new ExamineProcModel
                {
                    Id = Model.Id,
                    IsPassed = isPassed,
                    Comments = Model.Comments,
                    ContributionValue = Model.ContributionValue
                });
            //判断结果
            if (obj.Successful == false)
            {
                await PopupService.ToastErrorAsync("审核失败", obj.Error);
            }
            else
            {
                await PopupService.ToastSuccessAsync("审核成功", "对『" + (string.IsNullOrWhiteSpace(Model.ObjectName) ? ("Id：" + Model.ObjectId) : Model.ObjectName) + "』的审核已完成");
                //清除缓存
                if (_dataCacheService.UserContentCenterCatche?.PendingExamines != null)
                {
                    _dataCacheService.UserContentCenterCatche.PendingExamines.RemoveAll(s => s.Id == Model.Id);
                }
                NavigationManager.NavigateTo("/contentcenter");
            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "审核失败");
        }
    }
}
